﻿/// <reference path="Jquery/jquery-1.2.6-vsdoc.js" />
/// <reference path="Jquery/jquery-1.2.6.js" />

var IM = {};
IM.Control = {};
IM.Utility = {};


//IM基础类
IM.Base = function(conf) {
    this.Init = function() {
        if (conf)
            for (var i in conf) {
            this[i] = conf[i];
        }
    };
    this.Init();
};

//修改Date类的toString方法
Date.prototype.toString = function() {
    var format = function(num) { if (String(num).length == 1) return "0" + num; return num; };
    return this.getFullYear() + "-" + format(this.getMonth() + 1) + "-" + format(this.getDate()) + " " + format(this.getHours()) + ":" + format(this.getMinutes()) + ":" + format(this.getSeconds());
};

IM.GetCurrentTimeString = function() {
    return new Date().toString();
};

//从服务端返回的时间字符串获取时间对象
IM.GetTimeFromServerTimeString = function(str) {
    return new Date(parseInt(str.substring(6, str.length - 2)));
};

//鼠标信息
IM.Mouse =
{
    IsInitialed: false,
    Initial: function() {
        if (!this.IsInitialed) {

            var bodyonmousemove = (function(obj) {
                return function(e) {
                    //var xx = e.originalEvent.x || e.originalEvent.layerX || 0;
                    obj.Location.X = e.originalEvent.clientX;
                    obj.Location.Y = e.originalEvent.clientY;

                    obj.LayerLocation.X = e.originalEvent.x || e.originalEvent.layerX || 0;
                    obj.LayerLocation.Y = e.originalEvent.y || e.originalEvent.layerY || 0;
                };
            })(this);
            //$(document.body).unbind("mousemove");
            $(document.body).bind("mousemove", bodyonmousemove);
            this.IsInitialed = true;
        }
    },
    Location: { X: 0, Y: 0 }, //鼠标座标,获取前要先初始化
    LayerLocation: { X: 0, Y: 0 }
};

// 设置定时操作(直到运行结束才能停止) 间隔时间 总时间(如果totalTime为0则无限循环执行) 定时操作 结束回调
IM.SetInterval = function(interval, totalTime, targetMethod, callback) {

    var timer;
    var method = targetMethod;
    var usedTime = 0;

    var timerRun = function() {

        if (method)
            method(); //运行方法
        if (totalTime != 0)
            usedTime += interval; //统计总时间
        if (totalTime != 0 && usedTime >= totalTime) {//如果时间到则退出,如果totalTime为0则无限循环执行
            clearTimeout(timer);
            timer = null;
            if (callback)//回调
                callback();
            return;
        }
        timer = null;
        timer = setTimeout(timerRun, interval);
    };
    timer = null;
    timer = setTimeout(timerRun, interval);
};

//定时器类
IM.Timer = function(interval, totalTime) {
    this.Interval = interval; //时间间隔
    this.TotalTime = totalTime; //总时间(如果totalTime为0则无限循环执行)

    this.TargetMethod = null; //定时运行的方法
    this.CallBack = null; //运行结束回调方法

    var _started = false; //是否已经开始
    var _timer = null; //用来引用setTimeout产生的timer对象

    this.Start = function(interval, totalTime, targetMethod, callback) {//启动定时器
        this.Stop();
        if (interval)
            this.Interval = interval;
        if (totalTime)
            this.TotalTime = totalTime;
        if (targetMethod)
            this.TargetMethod = targetMethod;
        if (callback)
            this.CallBack = callback;

        _started = true;
        _runTimer(this); //运行定时方法
    };

    this.Stop = function() {//停止定时器
        _started = false;
        if (_timer) {
            clearTimeout(_timer);
            _timer = null;
        }
    };

    var _runTimer = function(obj) {//定时运行
        var usedtime = 0;

        var timerRun = function() {

            if (_started) {//如果开始标记是已经开始则进行定时操作
                if (obj.TargetMethod)
                    obj.TargetMethod(); //运行方法

                if (totalTime != 0)
                    usedtime += obj.Interval; //统计总时间

                if (totalTime != 0 && usedtime >= obj.TotalTime) {//如果时间到则退出,如果totalTime为0则无限循环执行
                    clearTimeout(_timer); //清除定时器

                    if (obj.CallBack)//定时结束执行回调
                        obj.CallBack();

                    _started = false;    //执行结束,开始标记变为false

                    _timer = null; //清空变量
                    usedtime = null;
                    return;
                }
                _timer = null;
                _timer = setTimeout(timerRun, obj.Interval);
            }
        };
        _timer = null;
        _timer = setTimeout(timerRun, obj.Interval);
    };

    this.Init = function() {
        if (!this.Interval)
            this.Interval = 50;
        if (!this.TotalTime)
            this.TotalTime = 0;
    };

    this.Init();
};

//事件添加
IM.AttachEvent = function(e_, event_name_s, func_f, capture_b) {
    if (document.addEventListener) {
        event_name_s = event_name_s.slice(2);
        //event_name_s = event_name_s.replace(/^on/i, ""); 
        e_.addEventListener(event_name_s, func_f, capture_b);
    }
    else if (document.attachEvent) {
        e_.attachEvent(event_name_s, func_f);
    }
};

IM.DetachEvent = function(e_, event_name_s, func_f, capture_b) {
    if (e_.removeEventListener) {
        event_name_s = event_name_s.slice(2);
        //event_name_s = event_name_s.replace(/^on/i, ""); 
        e_.removeEventListener(event_name_s, func_f, capture_b);
    }
    else if (e_.detachEvent) {
        e_.detachEvent(event_name_s, func_f);
    }
};


